#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sobjc-commons/defines.h>
#include <sobjc-commons/utils.h>
#include "command.h"

char *objc_cmd = NULL;

objc_opt_list objc_main_opts = {
  0,            /* cc */
  0,            /* phase */
  0,            /* verbose */
  0,            /* cc_verbose */
  0,            /* debug */
  0,            /* profile */
  1,            /* does_link */
  1,            /* does_asm */
  1,            /* does_compile */
  1,            /* does_translate */
  0,            /* saves_temps */
  0,            /* links_static */
  1,            /* links_baustein */
  NULL,         /* output */
  NULL,         /* load_paths */
  NULL,         /* libs */
  NULL,         /* lib_paths */
  0,            /* optimize_lv */
  0,            /* ansi */
  NULL,         /* flags */
  NULL,         /* compiler_opts */

  0,            /* syntax_only */
  0,            /* w_pedantic */
  0,            /* pedantic_errors */
  0,            /* no_warn */
  NULL,         /* warns */

  NULL,         /* cstr_class */
  0,            /* no_nil_recvs */
  0,            /* gen_decls */
  0,            /* inline_cache */
  0,            /* dtable */
  0,            /* no_strict_msg */
  0,            /* w_no_protocol */
  0,            /* w_undeclared_sel */
  0,            /* w_strict_sel_match */

  0,            /* nfiles */
  NULL,         /* files */
  0,            /* nargs */
  NULL,         /* args */
};

void
objc_print_verbose(const char *format, ...)
{
  va_list ap;

  if (objc_main_opts.verbose) {
    fprintf(stderr, "# ");
    va_start(ap, format);
    vfprintf(stderr, format, ap);
    va_end(ap);
    fprintf(stderr, "\n");
  }
}

void
objc_print_error(const char *format, ...)
{
  va_list ap;

  va_start(ap, format);
  fprintf(stderr, "%s: ", objc_cmd);
  vfprintf(stderr, format, ap);
  fprintf(stderr, "\n");
  va_end(ap);
  exit(1);
}

int
objc_invoke_cmd_verbose(objc_str_list_t args)
{
  char *s;

  if (objc_main_opts.verbose) {
    s = objc_str_list_join(args, " ");
    objc_print_verbose("exec: %s", s);
  }
  return objc_invoke_cmd_with_arg_list(args);
}

